package com.cat.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.cat.dto.EmployeeInfoDTO;
import com.cat.dto.PageDTO;
import com.cat.dto.UserInfoDTO;
import com.cat.entity.Department;
import com.cat.entity.Employee;
import com.cat.entity.UserInfo;
import com.cat.query.DepartmentQuery;
import com.cat.result.Result;
import com.cat.service.EmployeeService;
import com.cat.service.IDepartmentService;
import com.cat.vo.EmployeeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
@Tag(name = "员工管理")
public class EmployeeController {

    private final IDepartmentService departmentService;
    private final EmployeeService employeeService;

    /**
     * 查询部门
     *
     * @return
     */
    @GetMapping("/department")
    @Operation(summary = "查询部门")
    public Result<List<Department>> department() {
        List<Department> list = departmentService.list();

        return Result.success(list, "成功");
    }

    /**
     * 查询部门员工
     *
     * @param departmentQuery
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "查询部门员工")
    public Result<PageDTO> list(@RequestBody DepartmentQuery departmentQuery) {
        log.info("查询部门：{}", departmentQuery);
        PageDTO<EmployeeVO> list = employeeService.getByDepartmentId(departmentQuery);

        return Result.success(list);
    }

    /**
     * 修改员工密码
     */
    @PostMapping("/rest_password")
    @Operation(summary = "修改员工密码")
    public Result<String> restPassword(@RequestBody EmployeeInfoDTO employeeInfoDTO) {
        log.info("修改员工密码:{}", employeeInfoDTO.getId());
        String s = MD5.create().digestHex("123456");

        Employee build = Employee.builder()
                .id(employeeInfoDTO.getId())
                .password(s)
                .loginErrorCount(0)
                .status(1)
                .build();

        employeeService.updateById(build);

        return Result.success("成功");
    }

    /**
     * 编辑员工信息
     */
    @PostMapping("/edit")
    @Operation(summary = "编辑员工信息")
    public Result<String> edit(@RequestBody EmployeeInfoDTO employeeInfoDTO){
        log.info("修改员工：{}",employeeInfoDTO);
        Employee employee = BeanUtil.copyProperties(employeeInfoDTO, Employee.class);
        employeeService.updateById(employee);

        return Result.success("成功");
    }

    /**
     * 新增员工
     */
    @PostMapping("/add")
    @Operation(summary = "新增员工")
    public Result<String> add(@RequestBody EmployeeInfoDTO employeeInfoDTO){
        log.info("新增员工：{}",employeeInfoDTO);
        employeeService.add(employeeInfoDTO);

        return Result.success("成功");
    }

    /**
     * 修改用户状态
     * @param statusDTO
     * @return
     */
    @PostMapping("/change")
    @Operation(summary = "修改用户状态")
    public Result<String> changeStatus(@RequestBody EmployeeInfoDTO statusDTO) {
        Employee employeeInfo = new Employee();
        employeeInfo.setId(statusDTO.getId());
        employeeInfo.setStatus(statusDTO.getStatus());
        employeeService.updateById(employeeInfo);

        return Result.success("成功");
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @Operation(summary = "删除员工")
    public Result<String> delete(@RequestBody Map<String,List<Long>> ids){
        log.info("要删除的员工id:{}",ids);
        List<Long> id = ids.get("id");

        employeeService.removeById(id.get(0));

        return Result.success("成功");
    }


}
